package com.neusoft.dao.impl;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.neusoft.dao.UmsAdminDao;
import com.neusoft.model.UmsAdmin;
import com.neusoft.utils.ClassUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Connection;
import java.util.List;
import java.util.Map;

/**
 * @Author: duan
 * @Date: Create in 10:09 2020/10/28
 */
@Repository
public class UmsAdminDaoImpl implements UmsAdminDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public UmsAdminDaoImpl setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
        return this;
    }

    @Override
    public boolean insert(UmsAdmin bean) {
        String sql = ClassUtil.generateInsertSqlFromObject(bean, "ums_admin");

        return jdbcTemplate.update(sql, ClassUtil.getValueArrayFromObject(bean)) > 0;
    }

    @Override
    public boolean update(UmsAdmin bean) {
        String sql = "update ums_admin set password = ? where id = ?";

        return jdbcTemplate.update(sql, bean.getPassword(), bean.getId()) > 0;
    }

    @Override
    public boolean delete(Long id) {
        String sql = "delete from ums_admin where id = ?";
        return jdbcTemplate.update(sql, id) > 0;
    }

    @Override
    public UmsAdmin queryById(Long id) {
        String sql = "select * from ums_admin where id = ?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<UmsAdmin>(UmsAdmin.class), id);
    }

    @Override
    public List<UmsAdmin> queryList() {
        String sql = "select * from ums_admin where status = ?";

        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<UmsAdmin>(UmsAdmin.class), 1);
    }

    @Override
    public List<Map<String, Object>> queryListMap() {
        String sql = "select * from ums_admin a";
        sql += " left join ums_admin_login_log b on a.id = b.admin_id";

        return jdbcTemplate.queryForList(sql);
    }

    @Override
    public Integer getCount() {
        String sql = "select count(1) from ums_admin";

        return jdbcTemplate.queryForObject(sql, Integer.class);
    }
}
